{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "103d2d8b-7c37-4e80-b952-811916b4dc7e",
   "metadata": {},
   "source": [
    "Chapter 05\n",
    "# 数值转换\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c4ff394f-2102-47ae-b28e-84aa0a4a790d",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 88.8\n",
    "y = 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a8e4e8c6-65e6-4269-a7c2-d10ec556ff7c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "88\n"
     ]
    }
   ],
   "source": [
    "# 将浮点数转换为整数\n",
    "x_to_int = int(x)\n",
    "print(x_to_int)  # 88"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e6078462-c143-44fc-a55d-d7ff8bb7bd20",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8.0\n"
     ]
    }
   ],
   "source": [
    "# 将整数转换为浮点数\n",
    "y_to_float = float(y)\n",
    "print(y_to_float)  # 8.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c8d11740-abb4-4b03-8853-c1f7fd193399",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(8+0j)\n"
     ]
    }
   ],
   "source": [
    "# 将整数转换为复数\n",
    "y_to_complex = complex(y)\n",
    "print(y_to_complex)  # (8+0j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "19916c6d-e7e1-44c2-823a-7742caf389f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "88.8\n"
     ]
    }
   ],
   "source": [
    "# 将数字转换为字符串\n",
    "x_to_str = str(x)\n",
    "print(x_to_str)  # '88.8'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "994a2c02-c491-4bcb-834c-6aa389a42fc6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
